package edu.xpu.compute.recommend.service.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.xpu.compute.recommend.common.model.PageModel;
import edu.xpu.compute.recommend.entity.user.UserBorrow;
import edu.xpu.compute.recommend.repository.user.UserBorrowRepository;

@Service
@Transactional(readOnly=true)
public class UserBorrowService {
	@Autowired
	private UserBorrowRepository userBorrowRepository;

	/**
	 * 获取分页列表
	 * @param pageNumberStr
	 * @param pageSizeStr
	 * @param search
	 * @param sortField
	 * @param sortType
	 * @return
	 */
	public PageModel<UserBorrow> getPageByConditions(String pageNumberStr, String pageSizeStr, final UserBorrow search
			, String sortField, String sortType) {
		int number = 0,
			size = 0;
		try {
			number = Integer.parseInt(pageNumberStr);
		} catch (Exception e) {}
		try {
			size = Integer.parseInt(pageSizeStr);
		} catch (Exception e) {}
		size = size == 0 ? 10 : size;
		Sort sort = null;
		if (!StringUtils.isEmpty(sortField)) {
			try {
				sort = new Sort(StringUtils.isEmpty(sortType) ? Direction.ASC : (sortType.equals("ASC")) ? Direction.ASC : Direction.DESC, sortField);
			} catch (Exception e) {}
		} else {
			sort = new Sort(Direction.ASC, "createDate");
		}
		Pageable pageable = PageRequest.of(number, size, sort);
		Specification<UserBorrow> specification = new Specification<UserBorrow>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<UserBorrow> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				if (search != null && StringUtils.isNotBlank(search.getUserName())) {
					predicates.add(cb.like(root.get("userName").as(String.class), "%"+search.getUserName()+"%"));
				}
				if (search != null && search.getUserGuid() != null) {
					predicates.add(cb.equal(root.get("userGuid").as(Long.class), search.getUserGuid()));
				}
				if (search != null && StringUtils.isNotBlank(search.getBookName())) {
					predicates.add(cb.like(root.get("bookName").as(String.class), "%"+search.getBookName()+"%"));
				}
				if (predicates.size() == 0) {
					return null;
				}
				Predicate predicateArr[] = new Predicate[predicates.size()];
				return cb.and(predicates.toArray(predicateArr));
			}
		};
		Page<UserBorrow> page = userBorrowRepository.findAll(specification, pageable);
		List<UserBorrow> content = page.getContent();
		PageModel<UserBorrow> pageModel = new PageModel<UserBorrow>(page.getSize(), page.getNumber(), page.getTotalElements(), content);
		return pageModel;
	}
	
	/**
	 * 保存信息
	 * @param sysUser
	 * @return
	 */
	@Transactional(readOnly=false)
	public UserBorrow save(UserBorrow userBorrow) {
		UserBorrow borrow = userBorrowRepository.saveAndFlush(userBorrow);
		return borrow;
	}
	/**
	 * 根据id获取信息
	 * @param id
	 * @return
	 */
	public UserBorrow get(Long id) {
		Optional<UserBorrow> optional = userBorrowRepository.findById(id);
		return optional.get();
	}
	
	@Transactional(readOnly=false)
	public void deleteById(Long id) {
		userBorrowRepository.deleteById(id);
	}
}
